package com.yonyou.ucf.mdf.app.isvdxq.utils;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class YmsPropertyUtil implements EnvironmentAware {
    private static final String NULL_OBJECT = new String();
    private ConcurrentHashMap<String, String> propertyCache = new ConcurrentHashMap();
    private Environment environment;

    public YmsPropertyUtil() {
    }

    public void removePropertyCache(String key) {
        this.propertyCache.remove(key);
    }

    public String getProperty(String key, boolean system) {
        Assert.notNull(this.environment, "请在environment对象初始化后调用此方法");
        String val = (String)this.propertyCache.computeIfAbsent(key, (k) -> {
            String ret = system ? System.getProperty(k) : this.environment.getProperty(k);
            return null == ret ? NULL_OBJECT : ret;
        });
        return NULL_OBJECT == val ? null : val;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}